//
//  GoodCommentsViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/25.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GoodCommentsViewController.h"

#import "GoodsCommentModel.h"
#import "GoodsCommentCell.h"

@interface GoodCommentsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIView *leftFlagView;

@property (nonatomic,strong) UILabel *totalEvaluationLB;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSourceArr;

@property (nonatomic,assign) NSInteger currentPage;

@property (nonatomic,assign) NSInteger totalCommentCount;//评论总数量

@end

@implementation GoodCommentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topTitle = @"用户评价";
    self.dataSourceArr = [NSMutableArray array];
    self.currentPage = 1;
    
    
    [self initUI];
    [self getNewData];
}

#pragma mark - setter
- (void)setProductId:(NSInteger)productId{
    _productId = productId;
}

#pragma mark - data
-(void)getNewData{
    self.dataSourceArr = [NSMutableArray array];
    [NetWorkingShop getGoodsCommentListWithProductId:self.productId WithPage:self.currentPage WithSuccessBlock:^(id  _Nonnull responseObj) {
        [self.tableView.mj_header endRefreshing];
        NSDictionary *data = responseObj[@"data"];
        self.totalCommentCount = [data[@"total"] integerValue];
        NSArray *records = data[@"records"];
        for (NSDictionary *dic in records) {
            GoodsCommentModel *model = [GoodsCommentModel paraWithDict:dic withShowDetail:YES];
            [self.dataSourceArr addObject:model];
        }
        self.totalEvaluationLB.text = [NSString stringWithFormat:@"商品评价 (%ld)",(long)self.totalCommentCount];
        [self.tableView reloadData];
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
}
-(void)getMoreData{
    [NetWorkingShop getGoodsCommentListWithProductId:self.productId WithPage:self.currentPage WithSuccessBlock:^(id  _Nonnull responseObj) {
        [self.tableView.mj_footer endRefreshing];
        NSDictionary *data = responseObj[@"data"];
        self.totalCommentCount = [data[@"total"] integerValue];
        NSArray *records = data[@"records"];
        for (NSDictionary *dic in records) {
            GoodsCommentModel *model = [GoodsCommentModel paraWithDict:dic withShowDetail:YES];
            [self.dataSourceArr addObject:model];
        }
        
        self.totalEvaluationLB.text = [NSString stringWithFormat:@"商品评价 (%ld)",(long)self.totalCommentCount];
        
        [self.tableView reloadData];
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
    });
}

#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.leftFlagView];
    [self.view addSubview:self.totalEvaluationLB];
    [self.view addSubview:self.tableView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.currentPage = 1;
        [self getNewData];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.currentPage = self.currentPage + 1;
        [self getMoreData];
    }];
}

#pragma mark - lazy

- (UIView *)leftFlagView{
    if (!_leftFlagView) {
        _leftFlagView = [[UIView alloc] initWithFrame:CGRectMake(15, kHeight_NavBar+15, 4, 14)];
        _leftFlagView.backgroundColor = RGB(26, 128, 255);
        _leftFlagView.clipsToBounds = YES;
        _leftFlagView.layer.cornerRadius = 2;
    }
    return _leftFlagView;
}

- (UILabel *)totalEvaluationLB{
    if (!_totalEvaluationLB) {
        _totalEvaluationLB = [[UILabel alloc] initWithFrame:CGRectMake(self.leftFlagView.right+8, self.leftFlagView.top-3, ScreenWidth/2+20, 20)];
        _totalEvaluationLB.textColor = MainBlackTextColor;
        _totalEvaluationLB.font = FontSet(14);
        _totalEvaluationLB.text = @"商品评价 ";
    }
    return _totalEvaluationLB;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kHeight_NavBar+44, ScreenWidth, ScreenHeight-kHeight_NavBar-44+kSafe_Bottom) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.backgroundColor = UIColor.whiteColor;
        [_tableView setTableHeaderView:[self tableHeaderAndFooterView]];
        [_tableView setTableFooterView:[self tableHeaderAndFooterView]];
    }
    return _tableView;
}
-(UIView *)tableHeaderAndFooterView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1f)];
    return view;
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodsCommentModel *model = self.dataSourceArr[indexPath.row];
    return model.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodsCommentModel *model = self.dataSourceArr[indexPath.row];
    GoodsCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsCommentCell"];
    if (!cell) {
        cell = [[GoodsCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodsCommentCell"];
    }
    cell.model = model;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



@end
